package utils;
import java.sql.*;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

public class JdbcUtils {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://192.168.157.100:3306/BASH";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456Ljm!";
private static Integer INITIAL_POOL_SIZE = 5;
private static Integer MAX_POOL_SIZE = 10;
private static final LinkedList<Connection> CONNECTIONS_POOLS = new LinkedList<>();
private static final ReentrantLock LOCK = new ReentrantLock();

    static {
        try{
            Class.forName(DRIVER);
            for(int i=0; i<INITIAL_POOL_SIZE; i++){
                CONNECTIONS_POOLS.add(createConnection());
            }
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }
    }
    private static Connection createConnection() throws SQLException{
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }
   public static Connection getConnection() throws SQLException {
        LOCK.lock();
        try{
            if(CONNECTIONS_POOLS.isEmpty()){
                if(CONNECTIONS_POOLS.size()<MAX_POOL_SIZE){
                    CONNECTIONS_POOLS.add(createConnection());
                }
                else {
                    throw new SQLException("线程池已满");
                }
            }
            return CONNECTIONS_POOLS.removeFirst();
        }  finally {
            LOCK.unlock();
        }
   }
    public static void releaseConnection(Connection connection){
        LOCK.lock();
        try {
            if(connection != null){
                CONNECTIONS_POOLS.add(connection);
            }
        }finally {
            LOCK.unlock();
        }
    }

    public  void closeAllConnection(){
        LOCK.lock();
        try {
            for(Connection connection : CONNECTIONS_POOLS){
                if(connection != null && !connection.isClosed()){
                    try{
                        connection.close();
                    }catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
            CONNECTIONS_POOLS.clear();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            LOCK.unlock();
        }
    }
    public static void closeResource(ResultSet re, PreparedStatement ps, Connection connection){
        try{
            if(re != null){
                re.close();
            }
            if(ps != null){
                ps.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    public static void executeTransaction(RunnableWithResource rwr){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=getConnection();
            conn.setAutoCommit(false);
            rwr.run(conn,ps);
            conn.commit();
            System.out.println("事务提交。。。。。");
        }catch (SQLException e){
            if(conn!=null){
                try{
                    conn.rollback();
                    System.out.println("食物回滚。。。");
                }catch (SQLException e1){
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            closeResource(rs,ps,conn);
        }
    }

}